using UnityEngine;
using UnityEngine.UI;
using TMPro;

[RequireComponent(typeof(Image))]
[RequireComponent(typeof(AudioSource))]
public class ScreenTitleLevel : MonoBehaviour
{
	public delegate void EventListener();

	private const float TIMESTEP = 0.04f;

	private const float timeDispose = 0.3f;

	public TextMeshProUGUI titleStart;

	public TextMeshProUGUI titleComplete;

	private Image background;

	private AudioSource audioSource;

	private EventListener listenerCompelete;

	private float timerStart = 1.75f;

	private float timerComplete = 3f;

	private StartRectTransform titleStartPosition;

	private StartRectTransform titleCompletePosition;

	private float timeDisposeStep;

	private float timeDisposeMark;

	private float timerDispose;

	public void ShowStart()
	{
		titleStart.gameObject.SetActive(value: true);
		base.gameObject.SetActive(value: true);
		background = base.gameObject.GetComponent<Image>();
		background.raycastTarget = true;
		titleStartPosition = new StartRectTransform(titleStart.rectTransform);
		audioSource = base.gameObject.GetComponent<AudioSource>();
		audioSource.Play();
		if (titleComplete != null)
		{
			titleComplete.gameObject.SetActive(value: false);
		}
	}

	public void ShowStart(int numLevel)
	{
		ShowStart();
		titleStart.text = numLevel.ToString() + titleStart.text;
	}

	public void ShowCompelete(EventListener listener)
	{
		listenerCompelete = listener;
		background = base.gameObject.GetComponent<Image>();
		background.enabled = false;
		titleComplete.gameObject.SetActive(value: false);
		titleCompletePosition = new StartRectTransform(titleComplete.rectTransform);
		timerStart = 0f;
		base.gameObject.SetActive(value: true);
		Invoke("ShowCompeleteScreen", 5f);
		if (titleStart != null)
		{
			titleStart.gameObject.SetActive(value: false);
		}
	}

	public void ShowCompeleteScreen()
	{
		background = base.gameObject.GetComponent<Image>();
		background.enabled = true;
		titleComplete.gameObject.SetActive(value: true);
		audioSource = base.gameObject.GetComponent<AudioSource>();
		audioSource.Play();
	}

	public void Disable()
	{
		base.gameObject.SetActive(value: false);
	}

	private void OnDestroy()
	{
		titleStartPosition = null;
		titleCompletePosition = null;
	}

	private void Update()
	{
		if (Time.timeScale == 0f)
		{
			return;
		}
		if (timerStart != 0f)
		{
			timerStart -= Time.deltaTime / Time.timeScale;
			DisposeTitle(timerStart, titleStartPosition);
			if (timerStart <= 0f)
			{
				timerStart = 0f;
				base.gameObject.SetActive(value: false);
			}
		}
		else if (titleComplete != null && titleComplete.gameObject.activeSelf)
		{
			timerComplete -= Time.deltaTime / Time.timeScale;
			DisposeTitle(timerComplete, titleCompletePosition);
			if (timerComplete <= 0f)
			{
				listenerCompelete();
			}
		}
	}

	private void DisposeTitle(float timerCurrent, StartRectTransform titlePosition)
	{
		if (timeDisposeMark == 0f || timerDispose < timerCurrent)
		{
			timeDisposeMark = timerCurrent - 0.3f;
		}
		else if (timerCurrent <= timeDisposeMark)
		{
			if (timeDisposeStep != 0f)
			{
				timeDisposeStep = 0f;
				titlePosition.Reset();
			}
			return;
		}
		timerDispose = timerCurrent;
		float num = Mathf.Ceil(timerCurrent / 0.04f);
		if (timeDisposeStep != num)
		{
			timeDisposeStep = num;
			titlePosition.DisposeText();
		}
	}
}
